﻿using Batman.IdentityServer.Extension.Handers;
using Microsoft.AspNetCore.Authorization;
using System;
using System.Collections.Generic;
using System.Text;

namespace Microsoft.Extensions.DependencyInjection
{
    public static class PolicyServiceCollectionExtensions
    {
        /// <summary>
        /// 添加RBAC权限策略
        /// </summary>
        /// <param name="services"></param>
        /// <returns></returns>
        public static IServiceCollection AddRbacPolicy(this IServiceCollection services)
        {
            services.AddAuthorization(options =>
            {
                options.AddPolicy("rbac", builder =>
                {
                    builder.AddRequirements(new RbacRequirement());
                });
            });

            services.AddSingleton<IAuthorizationHandler, RbacAuthorizationHandler>();

            return services;
        }
    }
}
